<template>
  <div>
    <el-form :model="user" :rules="rules" ref="ruleFormRef">
      <el-form-item prop="account" class="user">
        <el-input placeholder="请输入账号" v-model="user.account" class="input-user"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input show-password placeholder="请输入密码" v-model="user.password" class="input-password"></el-input>
      </el-form-item>
    </el-form>

    <div class="checkBoxContainer">
      <div style="display: flex">
        <el-checkbox></el-checkbox>
        <span style="line-height: 32px;margin-left: 8px">下次自动登录</span>
      </div>
      <div  style="width: fit-content; margin-bottom: -4px"><span @click="getToPath('/forget')">忘记密码?</span></div>
    </div>

      <el-button type="primary" class="buttonLogin" @click="handleLogin(ruleFormRef)">登 录</el-button>
    <div class="toRegister">
      <div>还没有账号？<span @click="getToPath('/register')">去注册</span></div>
    </div>
  </div>

</template>

<script setup>

import router from "../router/index.js";
import {onMounted, ref} from "vue";
import { login, register} from "../api/user.js";
import {ElMessage} from "element-plus";
import userStore from '../stores/user.js'

const ruleFormRef = ref()
const userInfo = userStore()

const user = ref({
  account: '',
  password: ''
})

const rules = ref({
  account: [{required: true, message: "请输入账号", trigger: 'blur'}],
  password: [{required: true, message: "请输入密码", trigger: 'blur'}]
})


const getToPath = (path) => {
  router.push(path)
}

const handleLogin = async (formEl) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      login(user.value).then(res => {
        if (res.code === 200) {
          updateUserINfo(res.data)
          getToPath('/main/home')
          message('success', '登陆成功')
        } else {
          message('error', res.message)
        }
      }, error => {
        message('error', error.message)
      })
    } else {
      console.log('error submit!', fields)
    }
  })
}

const message = (type, text) => {
  ElMessage({
    message: text,
    type: type,
    plain: true,
  })
}

const updateUserINfo = (info) => {
  userInfo.name = info.name
  userInfo.type = info.type
  userInfo.infoNumber = info.infoNumber
  userInfo.studentNumber = info.studentNumber
  userInfo.password = info.password
  userInfo.accountNumber = info.accountNumber
  userInfo.id = info.id
}

</script>



<style scoped>
.user{
  margin-top: 20px
}
.input-user{
  height: 48px;
}
.input-password{
  height: 48px;
}

.buttonLogin {
  width: 100%;
  height: 48px;
}


.checkBoxContainer {
  display: flex;
  margin: 0 0 25px 0;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;

  > *:first-child {
    padding-top: 2px;
  }

  > div:nth-of-type(1) {
    font-size: 14px;
    color: #606266;
  }

  > div:nth-of-type(2) {
    width: 270px;
    text-align: right;

    > span {
      color: rgb(116, 120, 124);
      cursor: pointer
    }
  }
}

.toRegister {
  display: flex;
  flex-direction: row-reverse;
  font-size: 14px;

  > div {
    color: rgb(116, 120, 124);

    > span {
      color: #4285f4;
      cursor: pointer;
    }
  }
}

</style>
